package com.dormitory.management.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dormitory.management.system.entity.WeekCheckExcel;
import com.dormitory.management.system.req.WeekCheckPageReq;
import com.dormitory.management.system.resp.WeekCheckPageResp;
import com.dormitory.management.system.service.WeekCheckService;

import com.dormitory.management.system.utils.ExcelUtil;
import com.dormitory.managemnet.util.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 周例查表 前端控制器
 * </p>
 *
 * @author
 * @since 2021-12-17
 */
@Api(tags = "周例查管理")
@RestController
@RequestMapping("/weekCheck")
public class WeekCheckController {
    @Autowired
    WeekCheckService weekCheckService;

    @ApiOperation("上周星级寝室数量")
    @RequestMapping(value = "getCount",method = RequestMethod.POST)
    public String getCount(String username){return weekCheckService.count(username).toString();}

    @ApiOperation("周例查分页列表")
    @RequestMapping(value = "/getPage",method = RequestMethod.POST)
    public Result<IPage<WeekCheckPageResp>> getPage(@RequestBody WeekCheckPageReq req){
        return Result.ok(weekCheckService.getPage(req));
    }

    @ApiOperation("Excel导入评分表")
    @PostMapping("/createByExcel")
    @ResponseBody
    public Result createWeekCheck(String username,MultipartFile file)throws IOException {
        return weekCheckService.importWeekCheckByExcel(username,file);
    }

    @ApiOperation("Excel模板下载")
    @GetMapping("/excel/template")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "导入用户模板";
        String sheetName = "导入用户模板";
        List<WeekCheckExcel> weekCheckExcelList = new ArrayList<>();
        weekCheckExcelList.add(new WeekCheckExcel());
        try {
            ExcelUtil.writeExcel(response, weekCheckExcelList, fileName, sheetName, WeekCheckExcel.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
